import 'package:drm/base/appurl.dart';
import 'package:drm/base/page.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import 'bloc.dart';

class IpPage extends StatelessWidget {
  BuildContext _context;

  IpPage(this._context,{Key key}):super(key:key);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
            icon: Icon(
              Icons.arrow_back,
              color: Colors.white,
            ),
            onPressed: () {
              print('返回');
              Navigator.pop(_context);
            }),
        title: Text(
          '服务器设置',
          style: TextStyle(color: Colors.white),
        ),
        centerTitle: true,
        elevation: 0,
      ),
      body: MainPage(_context),
    );
  }
}

class MainPage extends StatefulWidget {
BuildContext _context;

MainPage(this._context,{Key key}):super(key:key);

  @override
  _MainPageState createState() => _MainPageState();
}

class _MainPageState extends PageState<MainPage> {
  IpsettingBloc _ipsettingBloc = IpsettingBloc();

  TextEditingController _ipController = TextEditingController();
  TextEditingController _portController = TextEditingController();

  String _ipErrorInfo ;
  String _portErrorInfo ;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    showLoading();
    _ipsettingBloc.dispatch(IpSettingGetEvent());

  }
   @override
  addBlocToBlocList() {
    getBlocs().add(_ipsettingBloc);
  }
  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _ipController.dispose();
    _portController.dispose();
  }

  @override
  Widget mainView(BuildContext context) {
    return BlocProviderTree(
        blocProviders: [
          BlocProvider<IpsettingBloc>(bloc: _ipsettingBloc),
        ],
        child: BlocBuilder(
            bloc: _ipsettingBloc,
            builder: (context, ipSettingState) {

              if ( ipSettingState is IpSettingSaveEventState) {
                hideLoading();
                if (ipSettingState.isGood) {
                 showToast('保存成功');
                 closePage();
                }else {
                  _ipErrorInfo = ipSettingState.ipError;
                  _portErrorInfo = ipSettingState.portError ;
                }
              }


              if (ipSettingState is IpSettingGetEventState) {
                hideLoading();
                AppIpPort appUrl = ipSettingState.appUrl;
                if ( appUrl != null) {
                  _ipController.text = appUrl.ip;
                  _portController.text = appUrl.port ;
                  _ipsettingBloc.dispatch(NullEvent());
                }


              }

              return Padding(
                padding: EdgeInsets.all(16),
                child: Column(
                  children: <Widget>[
                    iniIpEditView(context),
                    iniPortEditView(context),
                    iniButtonView(context)
                  ],
                ),
              );
            }));
  }

  Widget iniIpEditView(BuildContext context) {
    return TextField(
      keyboardType: TextInputType.number,
      controller: _ipController,
      maxLength: 16,
      decoration: InputDecoration(
          labelText: 'IP地址',
          prefixIcon: Icon(Icons.language),
          errorText: _ipErrorInfo),
    );
  }

  Widget iniPortEditView(BuildContext context) {
    return TextField(
      keyboardType: TextInputType.number,
      controller: _portController,
      maxLength: 4,
      decoration: InputDecoration(
          labelText: 'port地址',
          prefixIcon: Icon(Icons.exit_to_app),
          errorText: _portErrorInfo),
    );
  }

  Widget iniButtonView(BuildContext context) {
    return Align(
      child: SizedBox(
        height: 45.0,
        width: 300.0,
        child: RaisedButton(
          child: Text(
            '保存',
            style:
                TextStyle(color: Colors.white, letterSpacing: 10, fontSize: 20),
          ),
          color: Theme.of(context).primaryColor,
          onPressed: () {
            saveSetting();


          },
          shape: StadiumBorder(
              side: BorderSide(color: Theme.of(context).primaryColor)),
        ),
      ),
    );
  }

  void closePage() {
//    Navigator.pop(widget._context);
    Navigator.of(widget._context).maybePop();

  }

  void saveSetting() {
    showLoading();
    _ipsettingBloc.dispatch(
        IpSettingSaveEvent(appUrl:AppIpPort(_ipController.text, _portController.text)));
  }


}
